package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActAlertMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.UserActAlertDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserInfoDO;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.UserActAlertRequest;
import com.sztx.pay.center.rpc.api.domain.UserActAlertResponse;
import com.sztx.pay.center.rpc.api.service.UserActAlertRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.dataaccess.mysql.config.PageQuery;

@Service
public class UserActAlertRpcServiceImpl implements UserActAlertRpcService{
	@Autowired
	UserActAlertMysqlDAO userActAlertMysqlDAO;
	@Autowired
	ActService actService;
	@Autowired
	UserInfoService userInfoService;
	
	@Override
	public void save(UserActAlertRequest request) {
		checkParam(request);
		UserActAlertDO userActAlertDO = transferUserActAlertDO(request);
		UserActAlertDO haveActAlertDO = userActAlertMysqlDAO.findById(request.getUserId(), request.getActType());
		if(haveActAlertDO != null){
			throw new BusinessException("该账户已存在余额报警信息,不允许重复添加");
		}
		userActAlertMysqlDAO.save(userActAlertDO);
	}
	
	@Override
	public void update(UserActAlertRequest request) {
		checkParam(request);
		UserActAlertDO userActAlertDO = userActAlertMysqlDAO.findById(request.getUserId(), request.getActType());
		if(userActAlertDO == null){
			throw new BusinessException("未查询到该账户的余额报警信息");
		}
		UserActAlertDO updateActAlertDO = transferUserActAlertDO(request);
		userActAlertMysqlDAO.update(updateActAlertDO);
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<UserActAlertResponse> findByPage(
			UserActAlertRequest request, int pageNo, int pageSize) {
		ValidateUtil.ge(pageNo, 1, "pageNo必须大于0");
		ValidateUtil.ge(pageSize, 1, "pageSize必须大于0");
		UserActAlertDO userActAlertDO = new UserActAlertDO();
		userActAlertDO.setUserId(request.getUserId());
		userActAlertDO.setActType(request.getActType());
		userActAlertDO.setState(request.getState());
		
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		List<UserActAlertDO> userActAlertDOS = userActAlertMysqlDAO.listPage(userActAlertDO, pageQuery);
		List<UserActAlertResponse> list = new ArrayList<UserActAlertResponse>();
		for (UserActAlertDO tmpUserActAlertDO : userActAlertDOS) {
			UserActAlertResponse rep = new UserActAlertResponse();
			rep.setUserId(tmpUserActAlertDO.getUserId());
			rep.setActNo(tmpUserActAlertDO.getActNo());
			rep.setActType(tmpUserActAlertDO.getActType());
			rep.setUserName(tmpUserActAlertDO.getUserName());
			rep.setAlertBalance(tmpUserActAlertDO.getAlertBalance());
			rep.setAlertMobile(tmpUserActAlertDO.getAlertMobile());
			rep.setUpdateUser(tmpUserActAlertDO.getUpdateUser());
			rep.setState(tmpUserActAlertDO.getState());
			rep.setAlertTimes(tmpUserActAlertDO.getAlertTimes());
			rep.setAlertTotalTimes(tmpUserActAlertDO.getAlertTotalTimes());
			rep.setCreateTime(tmpUserActAlertDO.getCreateTime());
			rep.setUpdateTime(tmpUserActAlertDO.getUpdateTime());
			list.add(rep);
		}
		Integer totalPage = userActAlertMysqlDAO.listCount(userActAlertDO);
		PageModelResponse<UserActAlertResponse> response = new PageModelResponse<UserActAlertResponse>();
		response.setList(list);
		response.setPageNo(pageNo);
		response.setPageSize(pageSize);
		response.setTotal(list.size());
		response.setTotalPage(totalPage);
		return response;
		
	}

	private void checkParam(UserActAlertRequest request){
		String userId = request.getUserId();
		int actType = request.getActType();
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		UserActType userActType = UserActType.valueOf(actType);
		if(userActType == null){
			throw new BusinessException("账户类型错误");
		}
	}
	private UserActAlertDO transferUserActAlertDO(UserActAlertRequest request){
		String userId = request.getUserId();
		int actType = request.getActType();
		UserActType userActType = UserActType.valueOf(actType);
		UserActAlertDO userActAlertDO = new UserActAlertDO();
		userActAlertDO.setUserId(userId);
		userActAlertDO.setActType(actType);
		userActAlertDO.setAlertBalance(request.getAlertBalance());
		userActAlertDO.setAlertMobile(request.getAlertMobile());
		userActAlertDO.setUpdateUser(request.getUpdateUser());
		userActAlertDO.setState(request.getState());
		//需要查询账户接口补充其他字段
		UserActDO userActDO = actService.findUserAct(userId, userActType);
		if(userActDO == null){
			throw new BusinessException("未查询到用户【"+userId+"】的账户信息");
		}
		userActAlertDO.setActNo(userActDO.getActNo());
		UserInfoDO userInfoDO = userInfoService.findUserInfo(userId);
		userActAlertDO.setUserName(userInfoDO.getRealName());
		return userActAlertDO;
	}

}
